package com.hss01248.dialog.material;

import com.hss01248.dialog.ResourceTable;
import com.hss01248.dialog.Tool;
import com.hss01248.dialog.adapter.SuperLvHolder;
import com.hss01248.dialog.config.ConfigBean;
import ohos.agp.components.Component;
import ohos.agp.components.InputAttribute;
import ohos.agp.components.TextField;
import ohos.agp.components.Text.TextSizeType;
import ohos.app.Context;

/**
 * Created by huangshuisheng on 2017/11/18.
 */

public class MdInputHolder extends SuperLvHolder<ConfigBean> {
    TextField et1;
    TextField et2;
    ConfigBean bean;
    public MdInputHolder(Context context) {
        super(context);
    }

    @Override
    protected void findViews() {
        et1 = (TextField) rootView.findComponentById(ResourceTable.Id_et_1);
        et2 = (TextField) rootView.findComponentById(ResourceTable.Id_et_2);
    }

    @Override
    protected int setLayoutRes() {
        return ResourceTable.Layout_dialogutil_md_input;//R.layout.dialogutil_md_input;
    }

    @Override
    public void assingDatasAndEvents(Context context, ConfigBean bean) {
        this.bean = bean;
        setInputStyle(context,bean);

    }

    public String getTxt1(){
        if(et1!=null){
            return et1.getText().toString().trim();
        }else {
            return "";
        }
    }

    public String getTxt2(){
        if(et2!=null){
            return et2.getText().toString().trim();
        }else {
            return "";
        }
    }

    public TextField getEt1(){
        return et1;
    }

    public TextField getEt2(){
        return et2;
    }

    private void setInputStyle(Context context, ConfigBean bean) {
        if (Tool.isEmpty(bean.hint1)) {
            et1.setVisibility(Component.HIDE);
        } else {
            bean.setNeedSoftKeyboard(true);

            et1.setVisibility(Component.VISIBLE);
            et1.setHint(bean.hint1.toString());
            Tool.setTextColorById(et1, bean.inputTxtColor);

            et1.setTextSize(bean.inputTxtSize, TextSizeType.FP);
            if(!Tool.isEmpty(bean.inputText1)){
                et1.setText(bean.inputText1.toString());
                et1.setSelected(true);
            }

        }

        if (Tool.isEmpty(bean.hint2)) {
            et2.setVisibility(Component.HIDE);
        } else {
            bean.setNeedSoftKeyboard(true);
            et2.setVisibility(Component.VISIBLE);
            et2.setHint(bean.hint2.toString());
            Tool.setTextColorById(et2, bean.inputTxtColor);

            et2.setTextSize(bean.inputTxtSize, TextSizeType.FP);
            if (bean.isInput2HideAsPassword) {
                //设置EditText文本为可见的
                et2.setTextInputType(InputAttribute.PATTERN_PASSWORD);
            } else {
                //设置EditText文本为隐藏的
                et2.setTextInputType(InputAttribute.PATTERN_NULL);
            }
            if(!Tool.isEmpty(bean.inputText2)){
                et2.setText(bean.inputText2.toString());
                et2.setSelected(true);
            }
        }
    }

    @Override
    public void showKeyBoard(){
//弹出软键盘
        if(Tool.isEmpty(bean.hint2) && !Tool.isEmpty(bean.hint1)){
            Tool.showKeyBoard(et1);
        }else if(Tool.isEmpty(bean.hint1) && !Tool.isEmpty(bean.hint2)){
            Tool.showKeyBoard(et2);
        }else if(!Tool.isEmpty(bean.hint2) && !Tool.isEmpty(bean.hint1)){
            Tool.showKeyBoard(et1);
        }

    }

    @Override
    public void hideKeyBoard(){
        if(Tool.isEmpty(bean.hint2) && !Tool.isEmpty(bean.hint1)){
            Tool.hideKeyBoard(et1);
        }else if(Tool.isEmpty(bean.hint1) && !Tool.isEmpty(bean.hint2)){
            Tool.hideKeyBoard(et2);
        }else if(!Tool.isEmpty(bean.hint2) && !Tool.isEmpty(bean.hint1)){
            Tool.hideKeyBoard(et1);
        }
    }
}
